package com.netposa.datawarehouse.aggregator.client.search;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.netposa.appwebres.common.WebResInfo;
import com.netposa.datawarehouse.aggregator.client.VehicleServiceClient;
import com.netposa.datawarehouse.aggregator.client.fallback.BaseServiceClientFallback;
import com.netposa.datawarehouse.aggregator.search.ResourceParam;
import com.netposa.datawarehouse.aggregator.search.ResourceResult;
import com.netposa.datawarehouse.aggregator.web.model.DefaultCondition;
import com.netposa.datawarehouse.aggregator.web.model.FtrCondition;
import com.netposa.library.datawarehouse.aggregate.helper.ResourceDefined.ResourceInnerFiled;
import com.netposa.library.datawarehouse.aggregate.helper.ResourceDefined.ResourceType;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMethod;

@Component("vehicle")
public class VehicleResourceSearch extends AbstractResourceSearch {
   private final Logger logger = LoggerFactory.getLogger(VehicleResourceSearch.class);
   @Autowired
   private Gson gson;
   @Autowired
   private VehicleServiceClient vehicleServiceClient;
   @Autowired
   private BaseServiceClientFallback serviceClientFallback;

   public List<ResourceResult> search(List<ResourceParam> resourceParams){
      if (resourceParams != null && !resourceParams.isEmpty()) {
         List jlbhs = (List)Lists.transform(resourceParams, (input) -> {
            return input != null ? input.getRecordId() : null;
         }).stream().filter((s) -> {
            return !Strings.isNullOrEmpty(s);
         }).collect(Collectors.toList());
         JsonObject postParam = new JsonObject();
         postParam.add("jlbhs", this.gson.toJsonTree(jlbhs).getAsJsonArray());
         WebResInfo responseInfo = this.vehicleServiceClient.getByIdsWithVehicleService(postParam.toString());
         return this.rebuild(responseInfo, ResourceType.VEHICLE.getValue());
      } else {
         return new ArrayList();
      }
   }

   public List selectInParams(DefaultCondition defaultCondition) {
      WebResInfo responseInfo = this.vehicleServiceClient.listGcjl(JSON.toJSONString(defaultCondition));
      return this.rebuild(responseInfo, ResourceType.VEHICLE.getValue());
   }

   public List<ResourceResult> selectInParams2(RequestMethod requestMethod, String serviceName, String typeName, String path, String requestBody) {
      WebResInfo webResInfo = this.serviceClientFallback.handler(requestMethod, serviceName, path, requestBody);
      return this.rebuild(webResInfo, typeName);
   }

   public Integer count(DefaultCondition defaultCondition) {
      WebResInfo webResInfo = this.vehicleServiceClient.countVehicle(JSON.toJSONString(defaultCondition));
      if (webResInfo.getCode().equalsIgnoreCase("200") && webResInfo.getData() != null) {
         Double count = Double.valueOf(String.valueOf(webResInfo.getData()));
         return count.intValue();
      } else {
         this.logger.error("Request vehicle server failed {} = {} .", webResInfo.getCode(), webResInfo.getMessage());
         return 0;
      }
   }

   public List<ResourceResult> plateSearch(FtrCondition ftrCondition) {
      WebResInfo webResInfo = this.vehicleServiceClient.listGcjl(JSON.toJSONString(ftrCondition));
      return this.rebuild(webResInfo, ResourceType.VEHICLE.getValue());
   }

   public List<ResourceResult> rebuild(WebResInfo responseInfo, String type) {
      if (responseInfo.getCode().equalsIgnoreCase("200") && responseInfo.getData() != null) {
         JSONArray list = (JSONArray)responseInfo.getData();
         return Lists.transform(list, (input) -> {
            return this.convertResult((JSONObject)input);
         });
      } else {
         this.logger.error("Request vehicle server failed {} = {} .", responseInfo.getCode(), responseInfo.getMessage());
         return Lists.newArrayList();
      }
   }

   private ResourceResult convertResult(JSONObject input) {
      ResourceResult result = new ResourceResult();
      result.setData(input);
      result.getData().put(ResourceInnerFiled._TYPE.getFiled(), ResourceType.VEHICLE.getValue());
      result.getData().put(ResourceInnerFiled._RECORD_ID.getFiled(), input.get("recordId"));
      result.getData().put(ResourceInnerFiled._ABS_TIME.getFiled(), input.get("absTime"));
      result.getData().put(ResourceInnerFiled.SOURCE.getFiled(), input.get("source"));
      result.addRequestId(ResourceType.VEHICLE.getValue(), input.get("absTime").toString());
      return result;
   }
}
